
#ifndef _READWRITE_NOS_H_
#define _READWRITE_NOS_H_

#include <BMap>
#include <BStream>
#include <BNode>
#include <BLight>
#include <BCamera>
#include <BScene>
#include <BMesh>
#include <BGeometry>
#include <BMesh>
#include <BCone>
#include <BCube>
#include <BCylinder>
#include <BSphere>
#include <BTorus>
#include <BSurface>

#include <BImage>
#include <BFont>
#include <BDisplay>
#include <BMaterial>
#include <BTexture>
#include <BProgram>
#include <BShader>
#include <BUniform>

#include <BReadWrite>

#define CHUNK_NODE			10
#define CHUNK_LIGHT			13
#define CHUNK_CAMERA		15
#define CHUNK_MESH			17
#define CHUNK_GEOMETRY		18
#define CHUNK_ELEMENT		19

#define CHUNK_CONE			26
#define CHUNK_CUBE			27
#define CHUNK_CYLINDER		28
#define CHUNK_SPHERE		29
#define CHUNK_TORUS			31
#define CHUNK_SURFACE		33

#define CHUNK_FONT			100
#define CHUNK_PALETTE		102
#define CHUNK_IMAGE			108
#define CHUNK_DISPLAY		109
#define CHUNK_BITMAP		110
#define CHUNK_PIXMAP		112

#define CHUNK_STATESET		204
#define CHUNK_MATERIAL		205
#define CHUNK_MATERIAL_ID	206
#define CHUNK_TEXTURE		207
#define CHUNK_TEXTURE_ID	208
#define CHUNK_PROGRAM		209
#define CHUNK_PROGRAM_ID	210
#define CHUNK_SHADER		213
#define CHUNK_UNIFORM		214

#define CHUNK_OBJECT_MAX	299

//value
#define CHUNK_OBJECTDATA	300
#define CHUNK_NAME			301
#define CHUNK_ENABLE		302
#define CHUNK_VISIBLE		304
#define CHUNK_MATRIX		306
#define CHUNK_RECT			307
#define CHUNK_SPACE			308

#define CHUNK_SHAPEDATA		309
#define CHUNK_LIGHTING		310
#define CHUNK_DEPTHTEST		311
#define CHUNK_CULLBACK		312
#define CHUNK_NORMALIZE		313
#define CHUNK_LINESMOOTH	314
#define CHUNK_LINEWIDTH		315
#define CHUNK_POINTSMOOTH	316
#define CHUNK_POINTSIZE		317
#define CHUNK_POLYGONSMOOTH	318
#define CHUNK_POLYGONMODE	319
#define CHUNK_TRANSPARENT	320

#define CHUNK_COLOR			330
#define CHUNK_COLORMAP		331
#define CHUNK_COLLIDING		332

#define CHUNK_VERTICES		353
#define CHUNK_NORMALS		354
#define CHUNK_TEXCOORDS		355
#define CHUNK_FACES			356
#define CHUNK_AMBIENT		357
#define CHUNK_DIFFUSE		358
#define CHUNK_SPECULAR		359
#define CHUNK_EMISSION		360
#define CHUNK_SHININESS		361
#define CHUNK_ATTENUATION	362
#define CHUNK_ATTENUATION2	363
#define CHUNK_POSITION		364
#define CHUNK_UP			365
#define CHUNK_CENTER		366
#define CHUNK_WIDTH			367
#define CHUNK_NEAR			369
#define CHUNK_FAR			370
#define CHUNK_BACKGROUND	371
#define CHUNK_FILENAME		373
#define CHUNK_FORMAT		374
#define CHUNK_SIZE			375
#define CHUNK_PIXELS		376
#define CHUNK_ORIGIN		399
#define CHUNK_RIGHT			400
#define CHUNK_FRONT			401
#define CHUNK_RIGHTSEGMENT	402
#define CHUNK_FRONTSEGMENT	403
#define CHUNK_BLEND			450
#define CHUNK_FILTER		451
#define CHUNK_WRAP			452
#define CHUNK_BLENDCOLOR	453
#define CHUNK_MIPMAP		454
#define CHUNK_PRIMITIVE		460
#define CHUNK_FIRST			461
#define CHUNK_COUNT			462
#define CHUNK_INDICES		463
#define CHUNK_PIECES		464
#define CHUNK_TYPE			466
#define CHUNK_TEXT			467
#define CHUNK_SIGN			468

#define CHUNK_MAX			500

using namespace BWE;

class ReadWrite_nos : public BReadWrite
{
public:
	class Chunk
	{
	public:
		Chunk()
		{
			begin = 0;
			sign = 0;
			size = 0;
			end = 0;
		}
		int			begin;
		int			sign;
		int			size;
		int			end;
	};

public:
	ReadWrite_nos();
	~ReadWrite_nos();

	virtual bool readObject(const BString& fileName, BObject* object);
	virtual bool writeObject(const BString& fileName, const BObject* object);

	virtual const BString& author() const;
	virtual const BString& version() const;
	virtual const BString& description() const;
	virtual const BString& log() const;

private://read
	bool readBeginChunk(Chunk& chunk, int end);
	bool readEndChunk(Chunk& chunk);

	bool readObjectData(const Chunk& chunk, BObject* object);
	bool readShapeData(const Chunk& chunk, BShape* shape);
	bool readNodeData(const Chunk& chunk, BNode* node);

	BNode* readNode(const Chunk& chunk);
	BLight* readLight(const Chunk& chunk);
	BCamera* readCamera(const Chunk& chunk);

	BMesh* readMesh(const Chunk& chunk);
	BGeometry* readGeometry(const Chunk& chunk);
	BElement* readElement(const Chunk& chunk);

	BMaterial* readMaterial(const Chunk& chunk);
	BImage* readImage(const Chunk& chunk);
	BDisplay* readDisplay(const Chunk& chunk);
	BProgram* readProgram(const Chunk& chunk);
	BShader* readShader(const Chunk& chunk);
	BUniform* readUniform(const Chunk& chunk);

protected:
	bool writeSMTP(const BScene* scene, const BNode* node);

	bool writeObjectData(const BObject* object);
	bool writeNodeData(const BNode* node);
	bool writeShapeData(const BShape* shape);
	bool writeTextureData(const BTexture* texture);
	bool writeWidgetData(const BWidget* widget);

	bool writeNode(const BNode* node);
	bool writeLight(const BLight* light);
	bool writeCamera(const BCamera* camera);

	bool writeShape(const BShape* shape);
	bool writeMesh(const BMesh* mesh);
	bool writeGeometry(const BGeometry* geometry);
	bool writeElement(const BElement* element);

	bool writeCube(const BCube* cube);
	bool writeCylinder(const BCylinder* cylinder);
	bool writeSphere(const BSphere* sphere);
	bool writeTorus(const BTorus* torus);

	bool writeMaterial(const BMaterial* material);
	bool writeTexture(const BTexture* texture);
	bool writeImage(const BImage* image);
	bool writeDisplay(const BDisplay* display);
	bool writeProgram(const BProgram* program);
	bool writeShader(const BShader* shader);
	bool writeUniform(const BUniform* uniform);
	bool writeFont(const BFont* font);

private:
	BString			_author;
	BString			_version;
	BString			_description;
	BString			_log;

	BStream*				_stream;
	BArray<BMaterial*>		_materials;
	BArray<BTexture*>		_textures;
	BArray<BProgram*>		_programs;
};

#endif